package com.serverTechnology.teamsGroups.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.serverTechnology.resource.entity.ManagementEntity;
import com.serverTechnology.teamsGroups.entity.TeCassette;
import com.serverTechnology.teamsGroups.entity.dto.TeCassetteAddDto;
import com.serverTechnology.teamsGroups.entity.dto.TeCassetteDto;
import com.serverTechnology.teamsGroups.entity.dto.TeCassetteEditDto;
import com.serverTechnology.teamsGroups.entity.vo.TeCassetteVo;
import com.serverTechnology.teamsGroups.mapper.TeCassetteMapper;
import com.serverTechnology.teamsGroups.service.ITeCassetteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.RandomAccess;
import java.util.stream.Collectors;

/**
 * <p>
 * 料盒台账 服务实现类
 * </p>
 *
 * @author 王凯炫
 * @since 2024-08-05
 */
@Service
public class TeCassetteServiceImpl extends ServiceImpl<TeCassetteMapper, TeCassette> implements ITeCassetteService {

    @Resource
    private TeCassetteMapper teCassetteMapper;
    /**
     * 添加料盒台账
     *
     * @param teCassetteAddDto
     */
    @Override
    public void addTeCassette(TeCassetteAddDto teCassetteAddDto) {
        TeCassette teCassette = new TeCassette();
        BeanUtil.copyProperties(teCassetteAddDto, teCassette);
        try {
            this.save(teCassette);
        } catch (Exception e) {
            throw new RuntimeException("料盒编码重复");
        }
    }

    /**
     * 编辑料盒台账
     *
     * @param teCassetteDto
     */
    @Override
    public void editTeCassette(TeCassetteEditDto teCassetteDto) {
        TeCassette teCassette = new TeCassette();
        BeanUtil.copyProperties(teCassetteDto, teCassette);
        try {
            this.updateById(teCassette);
        } catch (Exception e) {
            throw new RuntimeException("料盒编码重复");
        }
    }

    /**
     * 分页查询料盒台账
     * @param teCassetteDto
     * @return
     */
    @Override
    public PageResult<TeCassetteVo> pageCassette(TeCassetteDto teCassetteDto) {
        // 查询
        LambdaQueryWrapper<TeCassette> like = new LambdaQueryWrapper<TeCassette>()
                .like(ObjectUtil.isNotEmpty(teCassetteDto.getCode()), TeCassette::getCode, teCassetteDto.getCode());
        // 将查询到的数据分页
        Page<TeCassette> teCassettePage = this.page(PageFactory.defaultPage(), like);
        // 将查询到的数据转换为Vo
        List<TeCassetteVo> teCassetteVos = BeanUtil.copyToList(teCassettePage.getRecords(), TeCassetteVo.class);
        // 返回
        Page<TeCassetteVo> teCassetteVoPage = new Page<>();
        // 将查询到的数据复制到要返回的Vo
        BeanUtils.copyProperties(teCassettePage, teCassetteVoPage);
        teCassetteVoPage.setRecords(teCassetteVos);

        return PageResultFactory.createPageResult(teCassetteVoPage);

    }


}
